﻿using System;

using System.Net;
using NWMES.MiModuleAutoInSetServiceService;
using NWMES.Mes;
using Newtonsoft.Json;
using NWMES;
using System.Configuration;

namespace MyMes.Mes
{

    /// <summary>
    /// 释放套号接口
    /// </summary>
    public class MesModuleAutoInSet : MESBaseService
    {
        static string DefaultApiSection = "MesModuleAutoInSet";
        public static NetworkCredential networkCredential = null;
        public static MesModuleAutoInSet Params = null;

        static MesModuleAutoInSet()
        {
            Params = config[DefaultApiSection].ToObject<MesModuleAutoInSet>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 调用Mes接口  释放套号
        /// </summary>
        /// <param name="SetNo"></param>套号
        /// <param name="InvBarcode"></param>传入条码清单
        /// <returns></returns>
        public static MesResult RequestMes(string SetNo, string[] InvBarcode,string ApiSection)
        {
            MiModuleAutoInSetServiceService miModuleAutoInSetServiceService;
            miModuleAutoInSetRequest MiModuleAutoInSetRequest;
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MesModuleAutoInSet>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification("ModeProcessSFC");
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }
            SetRequesetParams(out miModuleAutoInSetServiceService, out MiModuleAutoInSetRequest);

            //传入套号
            MiModuleAutoInSetRequest.setNo = SetNo;
            //传入条码清单
            MiModuleAutoInSetRequest.invBarcode = InvBarcode;
            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(MiModuleAutoInSetRequest));

            miAutoSetResponse Response = new miAutoSetResponse();
            miAutoSet MiAutoSet = new miAutoSet();
            MiAutoSet.ModuleSetRequest = MiModuleAutoInSetRequest;

            try
            {
                Response = miModuleAutoInSetServiceService.miAutoSet(MiAutoSet);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }
            SaveMesLog("MesModuleAutoInSet", result);
            return result;
        }

        /// <summary>
        /// 设置请求参数
        /// </summary>
        /// <param name="Service"></param>
        /// <param name="Request"></param>
        private static void SetRequesetParams(out MiModuleAutoInSetServiceService Service, out miModuleAutoInSetRequest Request)
        {
            Service = new MiModuleAutoInSetServiceService();
            Service.Url = Params.Url;
            Service.Timeout = Params.Timeout;
            Service.Credentials = networkCredential;
            Service.PreAuthenticate = true;

            Request = new miModuleAutoInSetRequest();
            Request.site = Params.Site;
            Request.userID = Params.User;
            Request.operation = Params.Operation;
            Request.activityID = Params.ActivityId;
            Request.resource = Params.Resource;
            Request.modeRel = Params.modeRel;
            Request.projectBom = Params.projectBom;
        }

        /// <summary>
        /// 释放套号
        /// </summary>
        public bool modeRel { get; private set; } = false;

        /// <summary>
        /// 系统
        /// </summary>
        public string projectBom { get; private set; } = "";

    }
}
